define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    

    /**
    说明：
        该类是所有oa列表的父类。负责数据绑定，分页等通用基础功能的实现。
        列表是基于rkListview实现。
    **/
    $.widget('rk.oaListCtrl', {
            options: {
                loadInfo: {}
            },
            _create: function(){
                var me = this;
                var elem = me.element;
       
            },
            myBody: null,
            myFooter: null,
            pagingBar: null,
            _init: function(){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                elem.html('<div class="oa-result-body"></div><div class="oa-result-footer"><div>');
                me.myBody = elem.find('>.oa-result-body');
                me.myFooter = elem.find('>.oa-result-footer');
                
                //将自己初始化为一个rkListview，实际的数据展示是在此做的
                me.myBody.rkListview({
                	onSelect: function(row,event){
                		me._handleRowSelect(row,event);
                	}
                });
            },
            loadData: function(loadinfo, callback){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                if(typeof loadinfo == 'undefined' || loadinfo == null){
                    //重新请求第一页
                    loadinfo = opt.loadInfo;
                    loadinfo.param.pageNo = 1;
                    if(me.pagingBar) me.pagingBar.rkPaging('changePage', 1, false);
                }
                if(loadinfo){
                    me.myFooter.hide();
                    me.myBody.rkListview('beBusy');
                    //return;
                    //if(me.pagingBar) me.pagingBar.hide();
                    rk.post(loadinfo.url, loadinfo.param).done(function(data){
                        if(data.status === 0){
                            data = data.data;
                            data = me._filterData(data);//将data转换为标准格式
                            me.bindData(data.results, loadinfo.rowTemplateId);
                            me._updatePaging(data.data_count);
                        }
                        me.myFooter.show();
                        if(typeof callback != 'undefined')(callback)();
                    });
                }

            },
            _filterData: function(data){
                return data;
            },
            _updatePaging: function(total){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                // if(total <= 20){
                //     return;
                // }

                if(!me.pagingBar){
                    me.pagingBar = me.myFooter.find('>div');
                    me.pagingBar.rkPaging({
                        pagenum: 1,
                        pagesize: (opt.loadInfo.pageSize ? opt.loadInfo.pageSize : 20),
                        rowscount: total,
                        onChange: function(pagenum){
                            opt.loadInfo.param.pageNo = pagenum;
                            if(opt.beforeChangePage)(opt.beforeChangePage)();
                            me.loadData(opt.loadInfo);
                            //重新加载数据时将旧的分页隐藏，视觉体验更好
                            me.myFooter.hide();
                        }
                    });
                }else{
                    me.pagingBar.rkPaging('updateTotal', total);
                }
				me.myFooter.show();
            },
            bindData: function(data, templateId){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                var tpl =  require.async(templateId, function(tpl){
                    if(me.myBody && me.myBody.data('rkListview'))
                    me.myBody.rkListview('bindData', data, tpl);
                });
                
            },
            setAutoload: function(conf){
                var me = this;
                var opt = me.options;
                opt.loadInfo = $.extend(true, opt.loadInfo, conf);
            },
            getAutoload: function(){
                var me = this;
                var opt = me.options;
                return opt.loadInfo;
            },
            showLoading: function(){
                var me = this;
                var elem = me.element;
            },
            hideLoading: function(){
                var me = this;
                var elem = me.element;
            },
            beBlank: function(text){
                var me = this;
                me.myBody.rkListview('beBlank', text);
            },
            _destroy: function(){
                var me = this;
            }
    });
});